package com.topisv.tms.service.task;

import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.task.DispatchOrderTask;
import com.topisv.tms.repository.task.DispatchOrderTaskDao;
import com.topisv.tms.service.EdiOrderService;
import com.topisv.tms.utils.InitProperties;

/**
 * 发货单任务逻辑类
 *  Date          Author       Version       Description
 * ----------------------------------------------------------
 *  2015-5-8      CC            1.0           Create
 */
@Component
public class DispatchOrderTaskService {
	@Autowired
	private DispatchOrderTaskDao dispatchOrderTaskDao;
	@Autowired
	private EdiOrderService ediOrderService;
	@PersistenceContext
	private EntityManager em;
	private String node;
	private int num;

	/**
	 * 锁定数据
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void lockData() {
		Query querySql = em.createNativeQuery("select t.id from tt_dispatch_order_task t  where deal_ip is null and deal_status=0 order by id desc limit ?1");
		querySql.setParameter(1, getNum());
		List<Long> idRs = querySql.getResultList();
		if (!idRs.isEmpty()) {
			Query updateSql = em.createNativeQuery("update tt_dispatch_order_task t set t.deal_ip = ?1,t.deal_time = now(),deal_status=1 where t.id in ?2 and deal_status=0");
			updateSql.setParameter(1, getNode());
			updateSql.setParameter(2, idRs);
			updateSql.executeUpdate();
		}
	}

	/**
	 * 加载数据
	 * @return List<DispatchOrderTask>
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public List<DispatchOrderTask> findData() {
		List<DispatchOrderTask> tasks = dispatchOrderTaskDao.findTask(getNode());
		if (!tasks.isEmpty()) {
			dispatchOrderTaskDao.update(getNode(), findIds(tasks));
		}
		return tasks;
	}

	private List<Long> findIds(List<DispatchOrderTask> tasks) {
		List<Long> rs = new ArrayList<Long>();
		for (DispatchOrderTask task : tasks) {
			rs.add(task.getId());
		}
		return rs;
	}

	/**
	 * 保存数据
	 * @param task
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void saveData(DispatchOrderTask task) {
		dispatchOrderTaskDao.save(task);
	}

	/**
	 * 处理数据
	 * @param task 
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void dealData(DispatchOrderTask task) throws Exception {
		ediOrderService.disPatchOrder(task, getNode());
		dispatchOrderTaskDao.save(task);
	}

	public String getNode() {
		if (node == null) {
			String name = ManagementFactory.getRuntimeMXBean().getName();
			node = name.substring(name.indexOf("@") + 1);
		}
		return node;
	}

	public int getNum() {
		if (num == 0) {
			String numParam = InitProperties.get("dispatchorder.loaddata.num");
			num = StringUtils.isBlank(numParam) ? 1 : Integer.valueOf(numParam);
		}
		return num;
	}

}
